/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.
 */

package jdbcmanager.print;

import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;

import javax.swing.*;

import jdbcmanager.app.*;
import jdbcmanager.print.preview.*;

/**
 * Printing and Print-Preview functionality.
 * 
 * @author Francisco Morero Peyrona
 */

public final class PrintShop
{
   private Printable  source;
   private PageFormat pgFormat;

   //-------------------------------------------------------------------------//
   
   /**
    * Class constructor.
    * 
    * @param source Any object that implements <code>Printable</code> interface.
    */
   public PrintShop( Printable source )
   {
      this.source   = source;
      this.pgFormat = PrinterJob.getPrinterJob().defaultPage();
   }
   
   /**
    * 
    * @param sTitle
    * @param dimSize
    */
   public void showPreview( String sTitle, Dimension dimSize )
   {
      App.setCursor( Cursor.WAIT_CURSOR );
      
      JDialog dlg = new PrintPreviewDialog( dimSize );
              dlg.setLocationRelativeTo( App.getFrameMain() );
              dlg.setTitle( (sTitle == null) ? "Print Preview" : sTitle.trim() );
              dlg.setVisible( true );
              
      App.restoreCursor();
   }
   
   /**
    * 
    * @param bShowPageDialog
    */
   public void print( boolean bShowPageDialog )
   {
      boolean bContinue = true;
      
      if( bShowPageDialog )
      {
         PrinterJob prnJob = PrinterJob.getPrinterJob();
         PageFormat pg     = prnJob.pageDialog( PrintShop.this.pgFormat );
         
         if( pg == this.pgFormat )
            bContinue = false;
         else
            this.pgFormat = pg;
      }
         
      if( bContinue )
      {
         PrinterJob prnJob = PrinterJob.getPrinterJob();
                    prnJob.setPrintable( PrintShop.this.source, PrintShop.this.pgFormat );           
         
         if( prnJob.printDialog() )
         {      
            try
            {
               prnJob.print();
            }
            catch( Exception exc )
            {
               App.showException( exc );
            }
         }
      }
   }
      
   //-------------------------------------------------------------------------//
   // INNER CLASS: Print Preview Dialog 
   //-------------------------------------------------------------------------//
   
   private final class PrintPreviewDialog extends JDialog
   {
      private PrintPreviewPanel pnlPrintPreview;
      
      PrintPreviewDialog( Dimension dimSize )
      {
         super( App.getFrameMain(), true );
         
         this.pnlPrintPreview = new PrintPreviewPanel( PrintShop.this.source, PrintShop.this.pgFormat, dimSize );
  
         setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE );
         getContentPane().setLayout( new BorderLayout() );
         getContentPane().add( createPreviewDialogToolBar()           , BorderLayout.NORTH  );
         getContentPane().add( new JScrollPane( this.pnlPrintPreview ), BorderLayout.CENTER );
         pack();
         
         Dimension dimOwner = getOwner().getSize();
         Dimension dimThis  = getSize();
         
         setSize( (int) Math.min( dimOwner.getWidth()  - 80, dimThis.getWidth()  ),
                  (int) Math.min( dimOwner.getHeight() - 60, dimThis.getHeight() ) );
      }
    
      JToolBar createPreviewDialogToolBar()
      {
         JButton  btnPrint = new JButton( new ImageIcon( getClass().getResource( "images/print.gif" ) ) );
                  btnPrint.setToolTipText( "Print" );
                  btnPrint.setMargin( new Insets( 4,6,4,6 ) );
                  btnPrint.addActionListener( new ActionListener()
                  {
                     public void actionPerformed( ActionEvent evt )
                     {
                        PrintShop.this.print( false );
                     }
                  } );

         JButton  btnPageSetup = new JButton( new ImageIcon( getClass().getResource( "images/page_setup.gif" ) ) );
                  btnPageSetup.setToolTipText( "Page Setup" );
                  btnPageSetup.setMargin( new Insets( 4,6,4,6 ) );
                  btnPageSetup.addActionListener( new ActionListener()
                  {
                     public void actionPerformed( ActionEvent evt )
                     {
                        PageFormat pfNew = PrinterJob.getPrinterJob().pageDialog( PrintShop.this.pgFormat );
                        
                        if( PrintShop.this.pgFormat != pfNew )  // If user press OK, a new instance is returned, otherwise the old one
                        {
                           PrintShop.this.pgFormat = pfNew;
                           pnlPrintPreview.setPageFormat( pfNew );
                        }
                     }
                  } );

         JLabel lblZoom = new JLabel( new ImageIcon( getClass().getResource( "images/zoom.gif" ) ) );
                lblZoom.setToolTipText( "Use the ComboBox to change zoom" );
       
         String[] asScale = { "10%", "25%", "50%", "75%", "100%", "125%" };
      
         JComboBox cmbScale = new JComboBox( asScale );
                   cmbScale.setToolTipText( "Increase / Decrease Zoom" );
                   cmbScale.setSelectedItem( this.pnlPrintPreview.getScale() + "%" );
                   cmbScale.setMaximumSize( cmbScale.getPreferredSize() );
                   cmbScale.setEditable( true );
                   cmbScale.addActionListener( new ActionListener()
                   {
                      public void actionPerformed( ActionEvent evt )
                      {
                         String str = ((JComboBox) evt.getSource()).getSelectedItem().toString().trim();
                        
                         if( str.endsWith( "%" ) )
                            str = str.substring( 0, str.length()-1 );
                        
                         try
                         {
                            int nScale = Integer.parseInt( str );
                            pnlPrintPreview.setScale( nScale );
                         }
                         catch( NumberFormatException exc )
                         {
                         }
                      }
                   } );

         JToolBar tb = new JToolBar(); 
                  tb.add( btnPrint );
                  tb.add( btnPageSetup );
                  tb.addSeparator( new Dimension( 28, 0 ) );
                  tb.add( lblZoom );
                  tb.add( cmbScale );

         return tb;
      }  
   }
}